package com.codeoss.framework.view.renderer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.expression.Expression;
import com.codeoss.framework.expression.IScriptService;
import com.codeoss.framework.expression.IScriptServiceStore;
import com.codeoss.framework.view.component.Component;
import com.codeoss.framework.view.component.Formula;
import com.codeoss.framework.view.service.IVariableService;

@Service
public class FormulaRenderer extends CommonComponentRenderer {
	@Autowired
	private IScriptServiceStore ScriptServiceStore;
	@Autowired
	private IVariableService variableService;
	protected void startRenderView(YunContext yunContext, Component component, ViewSerializeMap viewSerializeMap) {
		super.startRenderView(yunContext, component, viewSerializeMap);
		Formula formula = (Formula) component;
		viewSerializeMap.put("col", formula.getCol());
		viewSerializeMap.put("fieldcol", formula.getFieldCol());
		viewSerializeMap.put("noLabel", formula.isNoLabel());
		viewSerializeMap.put("dataField", formula.getDataField());
		Expression labelExpression = formula.getLabelExpression();
		if (labelExpression != null) {
			IScriptService scriptService=ScriptServiceStore.getScriptService(labelExpression);
			Object value = scriptService.eval(labelExpression.getScript(), yunContext.getVarValueMap());
			viewSerializeMap.put("label", value);
		}
	}
	
	@Override
	protected PropSerializeMap startRenderProp(YunContext yunContext, Component component, ViewPropMap viewPropMap) {
		PropSerializeMap propSerializeMap = super.startRenderProp(yunContext, component, viewPropMap);
		Formula formula = (Formula) component;
		
		Object value=variableService.getVariableValue(yunContext, formula.getVarName());
		propSerializeMap.put("value", value);
		return propSerializeMap;
	}
}
